const Column = require('../../../models/blog_column');

/**
 * 栏目管理控制器
 */
const ControllerColumn = {
    /* 获取列表 */
    getList: async function (req, res, next) {
        try {
            let { per_page, page, keywor, } = req.query;
            let pageSize = per_page ? per_page : 10;
            let params = {};
            let config = { pageSize, page };

            const columns = await Column.selectPaging(params, config);

            let list = columns.sort((a, b) => {
                return a.sort - b.sort
            })

            res.json({ code: 200, msg: '成功连接', data: { list } })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 获取详情 */
    getById: async function (req, res, next) {
        try {
            let { id } = req.query;

            const columns = await Column.select({ id });

            res.json({ code: 200, msg: '成功连接', data: columns[0] })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 添加 */
    Insert: async function (req, res, next) {
        try {
            let { title, name, path, keyword, desc } = req.body;
            let params = { title, name, path, created_time: new Date().getTime() };

            if (!name) return res.json({ code: 0, msg: '请输入栏目名称' });

            if (!path) return res.json({ code: 0, msg: '请输入路径' });

            if (keyword) params.keyword = keyword;

            if (desc) params.desc = desc;

            await Column.insert(params);

            res.json({ code: 200, msg: '成功连接', data: {} })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 编辑 */
    update: async function (req, res, next) {
        try {
            let { id, title, name, path, keyword, desc, display } = req.body;
            let params = {}
            if (display == 0 || display == 1) {
                params.display = display;
            } else {
                params = { title, name, path, update_time: new Date().getTime() };

                if (!name) return res.json({ code: 0, msg: '请输入栏目名称' });

                if (!path) return res.json({ code: 0, msg: '请输入路径' });

                if (keyword) params.keyword = keyword;

                if (desc) params.desc = desc;
            }

            await Column.update(id, params)

            res.json({ code: 200, msg: '修改成功', data: {} })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 删除 */
    del: async function (req, res, next) {
        try {
            let { id } = req.body;

            await Column.delete(id)

            res.json({ code: 200, msg: '成功连接', data: {} })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    },

    /* 排序 */
    onUpdateSort: async function (req, res, next) {
        try {
            let sortList = JSON.parse(req.body.sortList);

            sortList.forEach(async data => {
                let id = data.id;

                await Column.update(id, { sort: data.sort })
            })
            res.json({ code: 200, msg: '成功连接', data: {} })
        } catch (e) {
            res.json({ code: 0, data: e })
        }
    }
}

module.exports = ControllerColumn;